function generateColors(level) {
    try {
        // Base color - random hue
        const hue = Math.random() * 360;
        
        // Make saturation and lightness slightly different based on level
        // Start with more obvious differences and gradually decrease
        const baseSaturation = 50 + Math.random() * 20; // Base saturation between 50-70%
        const baseLightness = 45 + Math.random() * 20;  // Base lightness between 45-65%
        
        // Calculate initial differences (larger at start)
        const initialHueDiff = 30;    // Start with 30 degrees difference
        const initialSatDiff = 15;    // Start with 15% saturation difference
        const initialLightDiff = 15;  // Start with 15% lightness difference
        
        // Calculate final differences (same as before for maximum difficulty)
        const finalHueDiff = 2;      // Minimum 2 degrees difference
        const finalSatDiff = 1;      // Minimum 1% saturation difference
        const finalLightDiff = 1;    // Minimum 1% lightness difference
        
        // Level at which we reach maximum difficulty
        const maxDifficultyLevel = 50;
        
        // Calculate current differences based on level
        // Use exponential decay for smoother difficulty progression
        const progressFactor = Math.min(level / maxDifficultyLevel, 1);
        const easingFactor = 1 - Math.pow(1 - progressFactor, 3); // Cubic easing
        
        const hueDifference = initialHueDiff - (initialHueDiff - finalHueDiff) * easingFactor;
        const saturationDifference = initialSatDiff - (initialSatDiff - finalSatDiff) * easingFactor;
        const lightnessDifference = initialLightDiff - (initialLightDiff - finalLightDiff) * easingFactor;

        // Randomly decide which parameters to adjust
        const adjustments = [
            { param: 'hue', diff: hueDifference },
            { param: 'saturation', diff: saturationDifference },
            { param: 'lightness', diff: lightnessDifference }
        ];
        
        // For early levels (1-5), always include hue difference for easier distinction
        const numAdjustments = level <= 5 ? 2 : Math.floor(Math.random() * 2) + 1;
        const selectedAdjustments = level <= 5 
            ? [adjustments[0], ...adjustments.slice(1).sort(() => Math.random() - 0.5).slice(0, numAdjustments - 1)]
            : adjustments.sort(() => Math.random() - 0.5).slice(0, numAdjustments);

        // Generate the main color
        const mainColor = `hsl(${hue}, ${baseSaturation}%, ${baseLightness}%)`;

        // Generate the different color by applying selected adjustments
        let differentHue = hue;
        let differentSaturation = baseSaturation;
        let differentLightness = baseLightness;

        selectedAdjustments.forEach(adj => {
            switch (adj.param) {
                case 'hue':
                    differentHue = (hue + (Math.random() < 0.5 ? adj.diff : -adj.diff)) % 360;
                    if (differentHue < 0) differentHue += 360;
                    break;
                case 'saturation':
                    differentSaturation = Math.min(100, Math.max(0, 
                        baseSaturation + (Math.random() < 0.5 ? adj.diff : -adj.diff)
                    ));
                    break;
                case 'lightness':
                    differentLightness = Math.min(100, Math.max(0, 
                        baseLightness + (Math.random() < 0.5 ? adj.diff : -adj.diff)
                    ));
                    break;
            }
        });

        const differentColor = `hsl(${differentHue}, ${differentSaturation}%, ${differentLightness}%)`;

        return { mainColor, differentColor };
    } catch (error) {
        reportError(error);
        return { mainColor: 'hsl(0, 70%, 50%)', differentColor: 'hsl(10, 70%, 50%)' };
    }
}

function generateGrid(size, colors) {
    try {
        const totalCells = size * size;
        const differentColorPosition = Math.floor(Math.random() * totalCells);
        
        return Array(totalCells).fill(null).map((_, index) => ({
            color: index === differentColorPosition ? colors.differentColor : colors.mainColor,
            isDifferent: index === differentColorPosition
        }));
    } catch (error) {
        reportError(error);
        return [];
    }
}
